﻿using Sidvall.Serialization;
using System.Collections.Generic;
using System.Linq;

namespace Sidvall.UI.Entities
{
    public class TimeItemCollection : BusinessBaseCollection<TimeItem>
    {
        #region Public Members

        #region MaxCount

        private int _MaxCount = 10;
        public int MaxCount
        {
            get
            {
                return _MaxCount;
            }
            set
            {
                if (_MaxCount == value)
                    return;
                _MaxCount = value;
            }
        }

        #endregion

        #region AddOrUpdate

        public TimeItem AddOrUpdate(string id, string typeId, string name, System.DateTime time)
        {
            return AddOrUpdate(id, typeId, name, time, null, null);
        }
        public TimeItem AddOrUpdate(string id, string typeId, string name, System.DateTime time, object dataItem, string dataItemContext)
        {
            if (string.IsNullOrWhiteSpace(id))
                return null;
            var item = FindItem(typeId, id);
            if (item != null)
            {
                item.Name = name;
                item.Time = time;
                item.DataItem = dataItem;
                item.DataItemContext = dataItemContext;
            }
            else
            {
                item = new TimeItem()
                {
                    Id = id,
                    TypeId = typeId,
                    Name = name,
                    Time = time,
                    DataItem = dataItem,
                    DataItemContext = dataItemContext,
                };
                Add(item);
            }
            return item;
        }

        #endregion
        #region FindItem

        public TimeItem FindItem(string typeId, string id)
        {
            if (string.IsNullOrWhiteSpace(id))
                return null;
            if (!string.IsNullOrWhiteSpace(typeId))
            {
                return (from o in this
                        where o.Id.Equals(id, true, System.StringComparison.OrdinalIgnoreCase) && o.TypeId.Equals(typeId, true, System.StringComparison.OrdinalIgnoreCase)
                        select o).FirstOrDefault();
            }
            else
            {
                return (from o in this
                        where o.Id.Equals(id, true, System.StringComparison.OrdinalIgnoreCase)
                        select o).FirstOrDefault();
            }
        }

        #endregion
        #region Remove

        public bool Remove(string typeId, string id)
        {
            var item = FindItem(typeId, id);
            if (item != null)
                return Remove(item);
            return false;
        }
        public void Remove(string typeId, IEnumerable<string> ids)
        {
            if (ids == null)
                return;
            foreach (var id in ids)
                Remove(typeId, id);
        }

        #endregion
        #region Update

        public bool Update(string id, string typeId, string name, System.DateTime time)
        {
            var item = FindItem(typeId, id);
            if (item != null)
            {
                item.Name = name;
                item.Time = time;
                return true;
            }
            return false;
        }
        public bool Update(string id, string typeId, string name, System.DateTime time, object dataItem, string dataItemContext)
        {
            var item = FindItem(typeId, id);
            if (item != null)
            {
                item.Name = name;
                item.Time = time;
                item.DataItem = dataItem;
                item.DataItemContext = dataItemContext;
                return true;
            }
            return false;
        }

        #endregion

        #region InsertItem

        protected override void InsertItem(int index, TimeItem item)
        {
            base.InsertItem(index, item);
            while (this.Count > this.MaxCount)
            {
                var item2 = (from o in this
                             orderby o.Time
                             select o).FirstOrDefault();
                Remove(item2);
            }
        }

        #endregion

        #region Deserialize

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public static TimeItemCollection Deserialize(string value)
        {
            return Deserialize(value, null);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public static TimeItemCollection Deserialize(string value, int? maxCount)
        {
            return Sidvall.Serialization.SerializerManager.Current.TryDeserialize<TimeItemCollection>(value, () =>
            {
                var items = new TimeItemCollection();
                if (maxCount != null)
                    items.MaxCount = maxCount.Value;
                return items;
            });
        }

        #endregion
        #region Serialize

        public string Serialize()
        {
            return Serialize(this);
        }
        public static string Serialize(TimeItemCollection item)
        {
            return Sidvall.UI.Serialization.SerializerManager.Current.SerializeBusinessObject(item, Sidvall.Serialization.Formatting.None, true, true);
        }

        #endregion

        #endregion
        #region Constructors

        public TimeItemCollection()
        {
        }

        #endregion
    }
}
